import React from 'react';
import { WebVariable } from '@/service/WebVariable';

/**
 * 控制antdesign的tab权限
 * @param action 
 */
export const TabAuthRender = (action: string) => {
    let menuItem = sessionStorage.getItem("currentMenuItem");
    // 按钮权限未开启，不做控制。
    if (!WebVariable.useAuthButton) {
        return true;
    }
    if (typeof menuItem == "undefined" || menuItem == "" || menuItem == null) {
        return false;
    }
    let btnList = JSON.parse(menuItem).projectActionFunctionList;
    if (btnList.length < 1) {
        return false;
    };
    if (btnList.find(((t: { actionType: string; }) => t.actionType == action))) {
        return true;
    }
    return false;
}

//权限按钮控制
export class AuthRender extends React.Component<IAuthRenderProps>{
    render() {
        let menuItem = sessionStorage.getItem("currentMenuItem");
        // 按钮权限未开启，不做控制。
        if (!WebVariable.useAuthButton) {
            return this.props.children;
        }
        if (typeof menuItem == "undefined" || menuItem == "" || menuItem == null) {
            return <></>
        }
        let btnList = JSON.parse(menuItem).projectActionFunctionList;
        if (btnList.length < 1) {
            return <></>
        };
        if (btnList.find(((t: { actionType: string; }) => t.actionType == this.props.action))) {
            return this.props.children
        }
        return <></>
    }
}

interface IAuthRenderProps {
    action: string
}